package com.hmall.order.web;

import com.hmall.common.client.item.client.ItemClient;
import com.hmall.common.client.user.client.UserClient;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDTO;
import com.hmall.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("{id}")
    public Order queryOrderById(@PathVariable("id") Long orderId) {
        return orderService.getById(orderId);
    }

    @PostMapping
    public Map<String, String> save(@RequestBody OrderDTO orderDTO) {
        // TODO 完成下单逻辑; 需要考虑分布式事务

        // TODO 清理超时未支付订单, 异步: 延迟队列发信格式: rabbitTemplate.convertAndSend("order.delay", "dl", message, correlationData);

//        throw new RuntimeException("模拟的异常, 测试是否rollback ~");

        // TODO 响应结果封装形式; 订单id需要为String
         Map<String, String> map = new HashMap<>();

        log.info("{}", map);
        return map;
    }
}
